import client, auth, rest

def assert_all_in(in_list, all_list):
	assert all(item in in_list for item in all_list), "expected all items in %s to be found in %s" % (all_list, in_list)

def assert_equal(argv1, argv2):
	assert argv1.status == argv2, "Invalid response code %d : %r" % (argv1.status, argv1.reason)

def conver_path(filename):
	filename = '/'.join(filename.split('\\'))	
	return filename 
class DropBox():
	def login(self,usr, pwd):
	#def __init__(self):
		self.config = auth.Authenticator.load_config("config/testing.ini")
		self.dba = auth.Authenticator(self.config)
		print usr, pwd
		print self.config['testing_user'], self.config['testing_password']
		#self.access_token = self.dba.obtain_trusted_access_token(self.config['testing_user'], self.config['testing_password'])
		self.access_token = self.dba.obtain_trusted_access_token(usr, pwd)
		self.db_client = client.DropboxClient(self.config['server'], self.config['content_server'], self.config['port'], self.dba, self.access_token)
		self.root = self.config['root']

		self.CALLBACK_URL = 'http://printer.example.com/request_token_ready'
		self.RESOURCE_URL = 'http://' + self.config['server'] + '/0/oauth/echo'


	def delete(self,filename):

	#   db_client.file_delete(root, "/tohere")
		self.db_client.file_delete(root, filename)
		return db_client

	def upload(self,filename):
		print filename
		filename = conver_path(filename)
		print filename
		f = open(filename)
		resp = self.db_client.put_file(self.root, "/", f)
		assert resp
		assert_equal(resp, 200)

	def get_file(self,filename):
		resp = self.db_client.get_file(root, filename)
		assert_equal(resp.status, 200)
		assert len(resp.read()) > 100

	def account_info(self):
		resp = self.db_client.account_info()
		assert resp
		assert resp.status == 200, "Invalid response code %d : %r" % (resp.status, resp)
		print resp.body


	def fileops(filename):
		put_file()

		resp = db_client.file_create_folder(root, filename)
		assert_equal(resp.status, 200)
		assert_all_in(resp.data.keys(), [u'thumb_exists', u'bytes', u'modified', u'path',
										 u'is_dir', u'size', u'root', u'icon'])

		resp = db_client.file_copy(root, "/client_tests.py", "/tohere/client_tests.py")
		assert_equal(resp.status, 200)
		assert_all_in(resp.data.keys(), [u'thumb_exists', u'bytes', u'modified',
										 u'path', u'is_dir', u'size', u'root',
										 u'mime_type', u'icon'])

		resp = db_client.file_move(root, "/tohere/client_tests.py",
									  "/tohere/client_tests.py.temp")
		assert_equal(resp.status, 200)

		resp = db_client.file_delete(root, "/tohere/client_tests.py.temp")
		assert_equal(resp.status, 200)
		assert_all_in(resp.data.keys(), [u'is_deleted', u'thumb_exists', u'bytes', u'modified',
										 u'path', u'is_dir', u'size', u'root', u'mime_type', u'icon'])

	def metadata(filename):
		resp = db_client.metadata(root, "/tohere")
		assert resp
		assert_equal(resp.status, 200)
		assert_all_in(resp.data.keys(), [u'is_deleted', u'thumb_exists',
										 u'bytes', u'modified', u'path', u'is_dir',
										 u'size', u'root', u'hash', u'contents', u'icon'])
	def links():
		db_client = put_file()
		url = db_client.links(root, "/client_tests.py")
		assert_equal(url, "http://" + config['server'] + "/0/links/" + root + "/client_tests.py")

	def thumbnails():
		db_client.file_delete(root, "/sample_photo.jpg")

		f = open("tests/sample_photo.jpg", "rb")
		resp = db_client.put_file(root, "/", f)
		assert_equal(resp.status, 200)

		resp = db_client.get_file(root, "/sample_photo.jpg")
		body = resp.read()
		assert_equal(resp.status, 200)
		assert len(body) > 0

		resp = db_client.thumbnail(root, "/sample_photo.jpg", size='large')
		thumb = resp.read()
		assert_equal(resp.status, 200)
		assert len(body) > len(thumb)

